#include "FKCommonInstance.h"

#include "FKDataRecord.h"

#include <QFile>
#include <QDataStream>

FKCommonInstance::FKCommonInstance(QObject *parent):FKDataModel(parent){

}

FKCommonInstance::~FKCommonInstance(){
    qDeleteAll(_records);
}

FKCommonInstance* FKCommonInstance::loadFromFile(const QString &path, QObject *parent){
    QFile f(path);
    if(f.open(QIODevice::ReadOnly)){
        QDataStream stream(&f);
        stream.setVersion(QDataStream::Qt_5_0);
        QVariant source;
        stream>>source;
        if(stream.atEnd()){
            FKCommonInstance* instance=new FKCommonInstance(parent);
            instance->installNewSource(source);
            return instance;
        }
    }
    return 0;
}

bool FKCommonInstance::saveToFile(const QString &path) const{
    QFile f(path);
    if(f.open(QIODevice::WriteOnly)){
        QDataStream stream(&f);
        stream.setVersion(QDataStream::Qt_5_0);
        stream<<retriveData();
        return true;
    }
    return false;
}

FKDataRecord* FKCommonInstance::getCommonRecord(const qint32 &key) const{
    return _records.value(key,0);
}
